﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using loveMotorcade.web.Models;

namespace loveMotorcade.web.Controllers
{
    using Classes;
    using Filters;
    public class carClassController : Controller
    {
        //
        // GET: /carClass/

        public ActionResult Index()
        {
            ModelContainer db = new ModelContainer();
            ViewData.Model = db.carClass;
            return View();
        }

        //
        // 查询
        [OutputCache(Duration = 0)]
        [loginFilter()]
        public JsonResult searchEmploy(int id,DateTime dt) {
            DateTime AstarDt,AendDt,BstarDt,BendDt,CstarDt,CendDt,DstarDt,DendDt;
            AstarDt = new DateTime(dt.Year, dt.Month, dt.Day, 6, 0, 0);
            AendDt = new DateTime(dt.Year, dt.Month, dt.Day, 8, 0, 0);
            BstarDt = new DateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0);
            BendDt = new DateTime(dt.Year, dt.Month, dt.Day, 11, 0, 0);
            CstarDt = new DateTime(dt.Year, dt.Month, dt.Day, 13, 0, 0);
            CendDt = new DateTime(dt.Year, dt.Month, dt.Day, 17, 0, 0);
            DstarDt = new DateTime(dt.Year, dt.Month, dt.Day, 18, 0, 0);
            DendDt = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);

            ModelContainer db = new ModelContainer();
            var carc = db.carClass.First(d => d.ID == id);

            int An = carc.order.Where(d => (d.starTime <= AstarDt && d.endTime >= AstarDt) || (d.starTime <= AendDt && d.endTime >= AendDt)).Sum(s => s.employNum);
            int Bn = carc.order.Where(d => (d.starTime <= BstarDt && d.endTime >= BstarDt) || (d.starTime <= BendDt && d.endTime >= BendDt)).Sum(s => s.employNum);
            int Cn = carc.order.Where(d => (d.starTime <= CstarDt && d.endTime >= CstarDt) || (d.starTime <= CendDt && d.endTime >= CendDt)).Sum(s => s.employNum);
            int Dn = carc.order.Where(d => (d.starTime <= DstarDt && d.endTime >= DstarDt) || (d.starTime <= DendDt && d.endTime >= DendDt)).Sum(s => s.employNum);

            return myJson.success(new { A = carc.totalNum - An, B = carc.totalNum - Bn, C = carc.totalNum - Cn, D = carc.totalNum - Dn });
        }

        [OutputCache(Duration = 0)]

        [loginFilter()]
        public JsonResult ordering(int id,DateTime dt,string timeType,int num,string linkman,string linkPhone,string type){
            ModelContainer db = new ModelContainer();
            var carc = db.carClass.FirstOrDefault(d => d.ID == id);
            if (carc != null)
            {
                DateTime starDt, endDt;
                switch (timeType)
                {
                    case "A":
                        starDt = new DateTime(dt.Year, dt.Month, dt.Day, 6, 0, 0);
                        endDt = new DateTime(dt.Year, dt.Month, dt.Day, 8, 0, 0);
                        break;
                    case "B":
                        starDt = new DateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0);
                        endDt = new DateTime(dt.Year, dt.Month, dt.Day, 11, 0, 0);
                        break;
                    case "C":
                        starDt = new DateTime(dt.Year, dt.Month, dt.Day, 13, 0, 0);
                        endDt = new DateTime(dt.Year, dt.Month, dt.Day, 17, 0, 0);
                        break;
                    default:
                        starDt = new DateTime(dt.Year, dt.Month, dt.Day, 18, 0, 0);
                        endDt = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                        break;
                }
                int hasNum = carc.totalNum - carc.order.Where(dd => (
                    starDt >= dd.starTime && starDt <= dd.endTime) || (endDt >= dd.starTime && starDt <= dd.endTime)
                    ).Sum(s=>s.employNum);
                if (hasNum - num < 0)
                {
                    return myJson.error("预定数量超过库存");
                }
                else
                {
                    var _dt=DateTime.Now;
                    order o = new order()
                    {
                        carClassID = id,
                        createDate = _dt,
                        editDate = _dt,
                        date=_dt,
                        starTime = starDt,
                        endTime = endDt,
                        employNum = num,
                        linkman = linkman,
                        linkphone = linkPhone,
                        type = type,
                        userID = userC.LoginUser.ID,
                        disable = false,
                        orderNo="",

                    };
                    db.AddToorder(o);
                    db.SaveChanges();
                    return myJson.success("预定成功");
                }
            }
            else {
                return myJson.error("预定内部错误:不存在该车");
            }
        }
        [ValidateAntiForgeryToken()]

        public ActionResult siftOffset(DateTime dt,string timeType,int num)
        {
            
            DateTime starDt = dt, endDt = dt;
            switch (timeType)
            { 
                case "A":
                    starDt = new DateTime(dt.Year, dt.Month, dt.Day, 6, 0, 0);
                    endDt = new DateTime(dt.Year, dt.Month, dt.Day, 8, 0, 0);
                    break;
                case "B":
                    starDt = new DateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0);
                    endDt = new DateTime(dt.Year, dt.Month, dt.Day, 11, 0, 0);
                    break;
                case "C":
                    starDt = new DateTime(dt.Year, dt.Month, dt.Day, 13, 0, 0);
                    endDt = new DateTime(dt.Year, dt.Month, dt.Day, 17, 0, 0);
                    break;
                case "D":
                    starDt = new DateTime(dt.Year, dt.Month, dt.Day, 18, 0, 0);
                    endDt = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                    break;
            }
            
            ModelContainer db = new ModelContainer();
            ViewData.Model = db.carClass.Select<carClass, carClassSift>(d => new carClassSift() {
                basc = d,
                employNum = d.order.Where(dd => (
                    starDt >= dd.starTime && starDt <= dd.endTime) || (endDt >= dd.starTime && starDt <= dd.endTime)
                    ).Count() 
            }); 
            return View();
        }

        //
        // GET: /carClass/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /carClass/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /carClass/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /carClass/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /carClass/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /carClass/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }

}
